﻿using System.Data;
using System.Drawing;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using Mota.EditModule;

namespace Mota.XMLControls
{
    /// <summary>
    /// Represent ImageBOx based on DX PictureEdit 
    /// and WinFOrms ToolStrip 
    /// </summary>
    public class DXGuiImageBox:GuiImageBox
    {
        /// <summary>
        /// image holder
        /// </summary>
        private PictureEdit imageBox = new PictureEdit();
        /// <summary>
        /// toolstrip for edit menu
        /// </summary>
        private ToolStrip ts = new ToolStrip();
        /// <summary>
        /// Create WinForms GroupControl and add inside PictureEdit and ToolStrip
        /// set PictureAlignment to middlecenter
        /// and Show context menu to true
        /// 
        /// </summary>
        public DXGuiImageBox()
        {
            // add group control for toolstrip and image grouping
            visualControl = new GroupControl();

            (visualControl as GroupControl).Controls.Add(imageBox);
            // hide caption of the group control by default
            (visualControl as GroupControl).ShowCaption = false;
            // stretch image control inside group control
            imageBox.Dock = System.Windows.Forms.DockStyle.Fill;
            imageBox.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze;
            imageBox.Properties.PictureAlignment = ContentAlignment.MiddleCenter;
            imageBox.Properties.ShowMenu = true;
            imageBox.ImageChanged += _image_ImageChanged;
            imageBox.DataBindings.DefaultDataSourceUpdateMode = System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged;

            ts.Dock = DockStyle.Top;
            (visualControl as GroupControl).Controls.Add(ts);
            ToolStripButton tsb_open = new ToolStripButton();
            tsb_open.Image = new Bitmap(Resources.GetImage("5903B23F-12D2-415F-914E-FD991BD1DFA6"));
            ToolStripButton tsb_remove = new ToolStripButton();
            tsb_remove.Image = new Bitmap(Resources.GetImage("BDB8AD9C-7C43-4982-B0FA-A8263E83220E"));
            tsb_remove.Alignment = ToolStripItemAlignment.Right;
            ToolStripSeparator tss = new ToolStripSeparator();
            ToolStripButton tsb_fit = new ToolStripButton();
            tsb_fit.Image = new Bitmap(Resources.GetImage("D8DB2B28-111C-4B91-8185-DD3886E16858"));
            ToolStripButton tsb_fit1 = new ToolStripButton();
            tsb_fit1.Image = new Bitmap(Resources.GetImage("BBE006F6-FF52-432D-96BB-39E8B3978063"));

            ts.Items.AddRange(new ToolStripItem[] { tsb_open, tss, tsb_fit, tsb_fit1, tsb_remove });
            ts.ImageScalingSize = new Size(32, 32);

            tsb_open.Click += new System.EventHandler(loadBtnClick);
            tsb_remove.Click += new System.EventHandler(removeBtnClick);
            tsb_fit1.Click += new System.EventHandler(fit1Click);
            tsb_fit.Click += new System.EventHandler(fitClick);
        }
        /// <summary>
        /// Event handler for emagechanged event to write data into main dataset
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">EventArgs</param>
        private void _image_ImageChanged(object sender, System.EventArgs e)
        {
            // write value to databinding after image update
            if(imageBox.DataBindings["Image"]!=null)
                imageBox.DataBindings["Image"].WriteValue();
        }

        private void fitClick(object sender, System.EventArgs e)
        {
            imageBox.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
        }

        private void fit1Click(object sender, System.EventArgs e)
        {
            imageBox.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Clip;
        }

        private void removeBtnClick(object sender, System.EventArgs e)
        {
            imageBox.Image = null;
        }

        private void loadBtnClick(object sender, System.EventArgs e)
        {
            imageBox.LoadImage();
        }

        /// <summary>
        /// Set|GET GroupControl Text propertie
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as GroupControl).Text;
            }
            set
            {
                (visualControl as GroupControl).ShowCaption = false;
                (visualControl as GroupControl).Text = value;
            }
        }

        /// <summary>
        /// Add databinding to PictureEdit on Image propertie
        /// </summary>
        /// <param name="valueBinding">main binding source with current record data</param>
        public override void SetValue(object valueBinding)
        {
            if(valueBinding is System.Windows.Forms.BindingSource)
                (imageBox as PictureEdit).DataBindings.Add("Image", valueBinding as System.Windows.Forms.BindingSource,
                                                    XmlAttributes["XMLAttribute"], true);
        }
        /// <summary>
        /// Dummy propertie
        /// </summary>
        public override object Binding
        {
            get
            {
                return null;
            }
            set
            {
               // throw new System.NotImplementedException();
            }
        }

        public override object GetValue()
        {
            return (imageBox as PictureEdit).Image;
        }
    }
}
